package ajaxjs.service;

import java.util.HashMap;
import java.util.Map;
import ajaxjs.net.Net;
import ajaxjs.config.Application;

public abstract class Service extends Net {
	// 接口根地址
	private String baseUrl = "http://";

	public String getBaseUrl() {
		return baseUrl;
	}

	public void setBaseUrl(String baseUrl) {
		if(baseUrl.contains("http://")){
			this.baseUrl = baseUrl;
		}else{
			this.baseUrl += baseUrl;
		}
	}
	
	/**
	 * 每次请求都必须带上的参数
	 */
	private String getRequestPair(Map<String, String> requestPair) {

		String [] pairs = new String[requestPair.size()];
		
		int i = 0;
		for(String key : requestPair.keySet()){
			String pair = key + "=" + requestPair.get(key);
			pairs[i++] = pair;
		}
		
		return ajaxjs.Util.stringJoin(pairs, "&");
	}
	
	/**
	 * 生成接口最终的 URL
	 * @param baseUrl
	 * @param url
	 * @param baseParams
	 * @param params
	 * @return
	 */
	public String getServiceUrl(String baseUrl, String url, Map<String, String> baseParams, Map<String, String> params){
		String serviceUrl = null;
		
		if(params != null){
			serviceUrl = baseUrl + url + "?" + getRequestPair(baseParams) + "&" + getRequestPair(params);
		}else{
			serviceUrl = baseUrl + url + "?" + getRequestPair(baseParams); // for test or no args
		}
		
		return serviceUrl;
	}
	
	/**
	 * 虚方法
	 * @param url
	 * @param params
	 * @return
	 */
	public abstract String getServiceUrl(String url, Map<String, String> params);

	/**
	 * 获取单个对象，JSON 结构如 {"foo":bar};
	 * @param url
	 * @param params
	 * @return
	 */
	public Map<String, Object> getInfo(String url, Map<String, String> params){
		String fullUrl = getServiceUrl(url, params); // 注入！
		String jsCode = get(fullUrl);
		System.out.println("fullUrl:"+fullUrl);
		return Application.jsRuntime.NativeObject2Hash(jsCode);
	}

	public Map<String, Object> getInfo(String url, String id){
		if(id == null || "".equals(id))
			throw new IllegalArgumentException("没有 id 怎么能获取 info！？");
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", id);
		
		return getInfo(url, params);
	}
	
	public Map<String, Object> getInfo(String url, Integer id){
		return getInfo(url, id.toString());
	}
	
	/**
	 * 虚方法，返回列表接口所用的 key 是什么。
	 * @param url
	 * @param params
	 * @return
	 */
	public abstract String getTokenByKey(String key);
	
	public int lastQueryTotal; // 上次查询总数
	/**
	 * 获取列表数据，JSON 形如：{
	 *  total:33,
	 *  results:[
	 *  	{
	 *  		foo:bar,
	 *  		…… ……
	 *  	}
	 *  ]
	 * }
	 * @param url
	 * @param params
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object>[] getList(String url, Map<String, String> params){
		String fullUrl = getServiceUrl(url, params); // 注入！
		String jsCode = get(fullUrl);
		
		Map<String, Object> response = Application.jsRuntime.NativeObject2Hash(jsCode);
		
		if(response == null)return null;
		
		String _total = getTokenByKey("total"); // 获取总数
		Integer total = (Integer)response.get(_total);
		
		if(total != null){
			if(total <= 0){
				System.out.println("返回记录数位 0 ！");
				return null; // 存在总数字段，但为 0，查询结果为零，返回 null
			}
		}else return null; // 不存在总数字段，返回 null
		
		lastQueryTotal = total;
		
		Object result = response.get(getTokenByKey("result"));
		
		if(result == null){
			System.out.println("记录记录数：" + total + "。但没有数据列表返回！！！");
			return null;
		}

		return (Map<String, Object>[])result;
	}
	
	public Map<String, Object>[] getFlatList(String url, Map<String, String> params){
		String fullUrl = getServiceUrl(url, params); // 注入！
		String jsCode = get(fullUrl);
		
		return Application.jsRuntime.NativeArray2Map(jsCode);
	}	
}
